package com.lyn.module_custom_base.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

/**
 * created by lyn at 2021/10/11
 */
public abstract class BaseFragment<T extends ViewBinding> extends Fragment {

    public T binding;
    private ProgressDialog progressDialog;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = getBinding(inflater,container);
        setBinding(binding);
        setListener(binding);
        return binding.getRoot();
    }


    public abstract T getBinding(LayoutInflater inflater,ViewGroup container);

    public abstract void setBinding(T binding);

    public abstract void setListener(T binding);


    public void showToast(String message) {
        Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
    }

    public void showProgressBar(String title, String message) {
        if (progressDialog != null) {
            dismissProgressBar();
        }
        progressDialog = new ProgressDialog(getContext());
        progressDialog.setTitle(title);
        progressDialog.setMessage(message);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();
    }

    public void dismissProgressBar() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}
